﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DelonixRegiaHMSys.Core.Services;
using DelonixRegiaHMSys.Core.Models;
using DelonixRegiaHMSys.Infrastructure.Data;
using System.Web.UI.HtmlControls;

namespace DelonixRegiaHMSys.UI.WebForms
{
    public partial class MainPage : System.Web.UI.Page
    {
        string userID;
        protected void Page_Load(object sender, EventArgs e)
        {

                userID = Request.QueryString["UserID"];
                lblUserID.Text = userID;

                var saservice = new saServices(new EfSystemAccountQuery());
                bool IsAdminOrMS = saservice.IsAdminOrMS(userID);
                if (IsAdminOrMS == true)
                {
                    lblSchedule.Visible = true;
                    Adminbtn.Visible = true;
                }
                else
                {
                    lblSchedule.Visible = false;
                    Adminbtn.Visible = false;
                }
            if(!IsPostBack){
                BindGridView();
            }
           
        }

        private void BindGridView()
        {
            var roservice = new roServices(new EfRoomQuery());
            DataListRoom.DataSource = roservice.GetAll().ToList();
            DataListRoom.DataBind();
        }

        protected void DataListRoom_DataBinding(object sender, DataListItemEventArgs e)
        {
            HiddenField hf = (HiddenField)e.Item.FindControl("hroomStatus");
            LinkButton a = (LinkButton)e.Item.FindControl("lbtn");
            if (hf.Value == "V")
            {
                a.CssClass = "vacant";
            }
            else if (hf.Value == "O")
            {
                a.CssClass = "occupied";
            }
            else if (hf.Value == "SFC")
            {
                a.CssClass = "sfc";
            }
        }

        protected void lbtn_Click(object sender, EventArgs e)
        {
            string[] arg = new string[9];
            arg = ((LinkButton)sender).CommandArgument.ToString().Split(';');
            string roomNum = arg[0];
            string floorLevel = arg[1];
            string roomType = arg[2];
            string bedType = arg[3];
            string smokingOrNot = arg[4];
            string maxOccupancy = arg[5];
            string roomStatus = arg[6];
            string roomRates = arg[7];
            string guestID = arg[8];

            string querystring = "UserID=" + userID + "&RoomNum=" + roomNum + "&FloorLevel=" + floorLevel + "&RoomType=" + roomType + "&BedType=" + bedType + 
                "&SmokingOrNot=" + smokingOrNot + "&MaxOccupancy=" + maxOccupancy + "&RoomStatus=" + roomStatus + "&RoomRates=" + roomRates + "&GuestID=" + guestID;
            Server.Transfer("~/WebForms/SystemAccountModule/SpecificRoomDetails.aspx?" + querystring);
        }

        protected void lblRoomReservation_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/WebForms/RRandCIModule/ExistingGuestOrNot.aspx?UserID=" + userID);
        }

        protected void lblCheckIn_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/WebForms/RRandCIModule/ExistingGuestOrNot2.aspx?UserID=" + userID);
        }

        protected void lblReporting_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/WebForms/COandReportingModule/ReportsManagement.aspx?UserID=" + userID);
        }

        protected void lblSchedule_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/WebForms/HousekeepingandStaffModule/AssignSchedule.aspx?UserID=" + userID);
        }

        protected void Adminbtn_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/WebForms/SystemAccountModule/Administrator.aspx?User=" + userID);
        }

        protected void Logoutbtn_Click(object sender, EventArgs e)
        {
            Session["User"] = false;
            var saservice = new saServices(new EfSystemAccountQuery());
            saservice.UpdateUserStatus(userID, "OFF");
            Response.Redirect("~/WebForms/SystemAccountModule/LoginPage.aspx", true);
        }
    }
 }